using System;
using QAssetBundle;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace ProjectSurvival
{
    public class GamePanelData : UIPanelData
    {
    }

    public partial class GamePanel : UIPanel
    {
        public static EasyEvent FlashScreen = new EasyEvent();
        public static EasyEvent OnOpenTreasurePanel = new EasyEvent();
        protected override void OnInit(IUIData uiData = null)
        {
            mData = uiData as GamePanelData ?? new GamePanelData();
         
            Time.timeScale = 1f;

            LevelUpPanel.Hide();
            
            //注册升级时的回调
            Global.Level.Register((lv) =>
            {
                LevelUpPanel.Show();
                Time.timeScale = 0;
                AudioKit.PlaySound(Levelup_ogg.LevelUp);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            
            //注册经验值变更事件
            Global.Experience.RegisterWithInitValue(i =>
                {
                    var currentFillAmount = expValue.fillAmount;
                    var nextFillAmount = (float)i/Global.LevelUpToNextExperience();
                    ActionKit.Lerp(currentFillAmount, nextFillAmount, 0.2f, (value) =>
                    {
                        expValue.fillAmount = value; 
                    }).Start(this);
                })
                .UnRegisterWhenGameObjectDestroyed(gameObject);
            //注册等级变更事件
            Global.Level.RegisterWithInitValue(i =>
                {
                    LevelText.text = "等级：" + i.ToString();
                })
                .UnRegisterWhenGameObjectDestroyed(gameObject);

            
            Global.Experience.Register(i =>
            {
                if (Global.Experience.Value >= Global.LevelUpToNextExperience())
                {
                    Global.Experience.Value -= Global.LevelUpToNextExperience();
                    Global.Level.Value++;
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            Global.CurrentTime.RegisterWithInitValue((t) =>
            {
                if (Time.frameCount % 30 == 0)
                {
                    int currentSecondsInt = Mathf.FloorToInt(t);
                    int seconds = currentSecondsInt % 60;
                    int minutes = currentSecondsInt / 60;
                    timeText.text = $"时间：{minutes:00}:{seconds:00}";
                }
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            EnemyGenerator enemyGenerator = FindObjectOfType<EnemyGenerator>();

            //判断是否通关游戏
            ActionKit.OnUpdate.Register(() =>
            {
                if (enemyGenerator.IsLastWave && EnemyGenerator.EnemyCount.Value == 0 &&
                    enemyGenerator.CurrentWave == null && Global.CurrentTime.Value > 30)
                {
                    UIKit.OpenPanel<GamePassPanel>();
                }

                Global.CurrentTime.Value += Time.deltaTime;
            }).UnRegisterWhenGameObjectDestroyed(gameObject);

            EnemyGenerator.EnemyCount.RegisterWithInitValue((enemyCount) =>
            {
                enemyCountText.text = $"剩余敌人：{enemyCount.ToString()}";
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            Global.Coin.RegisterWithInitValue((coinCount) =>
            { 
                CoinCountText.text = $"金币数量：{coinCount.ToString()}";
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            
            //炸弹爆炸时闪屏
            FlashScreen.Register(() =>
            {
                ActionKit
                    .Sequence()
                    .Lerp(0, 0.5f, 0.1f, (alpha) =>
                    {
                        Color color = ScreenFlashImage.color;
                        color.a = alpha;
                        ScreenFlashImage.color = color;
                    })
                    .Lerp(0.5f, 0, 0.2f, alpha =>
                    {
                        Color color = ScreenFlashImage.color;
                        color.a = alpha;
                        ScreenFlashImage.color = color;
                    })
                    .Start(this);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
            OnOpenTreasurePanel.Register(() =>
            {
                Time.timeScale = 0f;
                TreasurePanel.Show();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        // private void OnGUI()
        // {
        //     GUILayout.Label($"coinPercent{Global.CoinPercent.Value.ToString("0.00")}");
        //     GUILayout.Label($"expPercent{Global.ExpPercent.Value.ToString("0.00")}");
        // }


        protected override void OnOpen(IUIData uiData = null)
        {
        }

        protected override void OnShow()
        {
        }

        protected override void OnHide()
        {
        }

        protected override void OnClose()
        {
        }
    }
}